﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using Outlook = Microsoft.Office.Interop.Outlook;
using RightClickExtension.Properties;


namespace RightClickExtension
{
    class Helper
    {
        /// <summary>
        /// Writes to error log.
        /// </summary>
        /// <param name="ex">The ex.</param>
        /// <param name="message">The message.</param>
        public static void WriteToErrorLog(Exception ex, string message)
        {
            string logLocation = Path.Combine(Application.StartupPath, "Errorlog.txt");
            if (!File.Exists(logLocation))
                File.Create(logLocation);
            using (StreamWriter sw = File.AppendText(logLocation))
            {
                sw.WriteLine(message);
                sw.WriteLine(ex.ToString());
                if (ex.StackTrace != null)
                    sw.WriteLine(ex.StackTrace.ToString());
            }
        }

        /// <summary>
        /// Writes the action to log.
        /// </summary>
        /// <param name="actionText">The action text.</param>
        public static void WriteActionToLog(string actionText)
        {
            string logLocation = Path.Combine(Application.StartupPath, "Log.txt");
            if (!File.Exists(logLocation))
                File.Create(logLocation);
            using (StreamWriter sw = File.AppendText(logLocation))
            {
                sw.WriteLine(actionText);
            }

        }      
        public static void SendFileByEmail(string filePath,string fileName)
        {
            Outlook.Application oApp = new Outlook.Application();
            Outlook.MailItem email = (Outlook.MailItem)
            (oApp.CreateItem(Outlook.OlItemType.olMailItem));
            if (File.Exists(filePath))
            {

                try
                {
                    email.Subject = "";
                    email.BodyFormat =
                    Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML;
                    email.Importance = Outlook.OlImportance.olImportanceNormal;
                    email.Attachments.Add(filePath, Outlook.OlAttachmentType.olByValue, 1,fileName);
                    WriteActionToLog(string.Format(Resources.strEmailFormat, filePath, DateTime.Now.ToString("dd-mm-yyyy HH:mm:ss")));
                    email.Display(true);
                }
                catch (Exception ex)
                {
                    WriteToErrorLog(ex, "error sending email");
                    MessageBox.Show("error sending email");
                    return;
                }
                finally
                {

                }
            }
        }
    }


}
